# -*- coding:utf-8 -*-


__author__ = 'weikai'

import datetime
import threading
import traceback

import gevent
from gevent import monkey

from common.log import Logger
from request_ancheng.request_premium import get_premium
from request_cic.getCode import get_citycode
from  request_cic.request_getdata import getData, getData_CPlateNo
from request_cicc.cicc_standard_flow.flow_main import flow_main
from request_huanong.hn_entrance import hn_request
from request_pingan.entrance import pingan_request
from session import getSession

monkey.patch_socket()
monkey.patch_all()
monkey.patch_ssl()


# 车架号查询信息之后查询保费
def getSource(dt):
    log = Logger()
    try:
        sessiondt = ""
        sessiondt = getSession()
        requestcic = sessiondt['4']
        loginRes = sessiondt['5']
        pingansession = sessiondt['1']
        ancheng_session = sessiondt['12']
        hn_session = sessiondt['13']
        vinNo = dt.get('vinNo', '')
        insureCarId = dt.get('insureCarId', '')
        if vinNo == '' or insureCarId == '':
            return
        dt['endDate'] = dt.get("preInsureEndDate", "")

        if dt.get('identitCard', '') == '':
            dt['identitCard'] = '320123199106102810'
        dt['insuranceType'] = dt.get("insuranceType", "")
        client = dt['client']

        log.info(u"程序开始运行%s" % datetime.datetime.now())
        try:

            # threads = []
            # th = threading.Thread(target=getData, args=(requestcic, dt))
            # th.start()
            # threads.append(th)
            # th = threading.Thread(target=flow_main, args=(loginRes, dt))
            # th.start()
            # threads.append(th)
            # th = threading.Thread(target=pingan_request, args=(pingansession, dt))
            # th.start()
            # threads.append(th)
            # th4 = threading.Thread(target=get_premium, args=(ancheng_session, dt))
            # th4.start()
            # threads.append(th4)
            # th5 = threading.Thread(target=hn_request, args=(hn_session, dt))
            # th5.start()
            # threads.append(th5)
            # for th in threads:
            #     th.join()

            companyId = dt.get('companyId', [])

            g_list = []
            if not companyId:
                g1 = gevent.spawn(getData, requestcic, dt)
                g2 = gevent.spawn(flow_main, loginRes, dt)
                g3 = gevent.spawn(pingan_request, pingansession, dt)
                g4 = gevent.spawn(get_premium, ancheng_session, dt)
                g5 = gevent.spawn(hn_request, hn_session, dt)
                gevent.joinall([g1, g2, g3, g4, g5])

            if companyId and '4' in companyId:
                g1 = gevent.spawn(getData_CPlateNo, requestcic, dt)
                g_list.append(g1)

            if companyId and '5' in companyId:
                g2 = gevent.spawn(flow_main, loginRes, dt)
                g_list.append(g2)

            if companyId and '1' in companyId:
                g3 = gevent.spawn(pingan_request, pingansession, dt)
                g_list.append(g3)

            if companyId and '12' in companyId:
                g4 = gevent.spawn(get_premium, ancheng_session, dt)
                g_list.append(g4)

            if companyId and '13' in companyId:
                g5 = gevent.spawn(hn_request, hn_session, dt)
                g_list.append(g5)

            if g_list:
                gevent.joinall(g_list)

            print u"程序结束运行%s" % datetime.datetime.now()

        except Exception, e:
            log.error(e)
            log.error(traceback.format_exc())

    except Exception as e:
        log.error(e)
        log.error(traceback.format_exc())


# 根据车牌号进行查询保费信息
def plateNo_getSource(dt):
    log = Logger()
    try:
        requestcic = dt['4']
        loginRes = dt['5']
        pingansession = dt['1']
        ancheng_session = dt['12']
        hn_session = dt['13']

        cdt = {}
        cdt['identitCard'] = dt['identifyNumber']
        cdt['custName'] = dt['insuredName']
        cdt['plateNumber'] = dt['licenseNo']
        cdt['vinNo'] = dt['vinNo']
        cdt['engineNo'] = dt['engineNo']
        cdt['insureCarId'] = dt['insureCarId']
        cdt['cityCode'] = get_citycode(dt['licenseNo'])
        cdt['endDate'] = dt['endDate']
        cdt['firstRegister'] = dt['enrollDate']
        cdt['vehicleBrand'] = dt['brandName']
        cdt['NSeatNum'] = dt.get('NSeatNum', 999)
        cdt['client'] = dt['client']
        client = cdt['client']
        cdt['sessionId'] = dt['sessionId']
        cdt['isPhone'] = dt['isPhone']
        cdt['insuranceType'] = dt['insuranceType']
        # cdt['insureTypeGroupId']=dt['insureTypeGroupId']

        log.info(u"程序开始运行%s" % datetime.datetime.now())
        try:
            '''
            # 使用多线程调用
            threads = []
            th1 = threading.Thread(target=getData_CPlateNo, args=(requestcic, cdt))
            th1.start()
            threads.append(th1)
            th2 = threading.Thread(target=flow_main, args=(loginRes, cdt))
            th2.start()
            threads.append(th2)
            th3 = threading.Thread(target=pingan_request, args=(pingansession, cdt))
            th3.start()
            threads.append(th3)
            th4 = threading.Thread(target=get_premium, args=(ancheng_session, cdt))
            th4.start()
            threads.append(th4)
            th5 = threading.Thread(target=hn_request, args=(hn_session, cdt))
            th5.start()
            threads.append(th5)

            for th in threads:
                th.join()
            '''

            companyId = dt.get('companyId', [])

            g_list = []
            if not companyId:
                g1 = gevent.spawn(getData_CPlateNo, requestcic, cdt)
                g2 = gevent.spawn(flow_main, loginRes, cdt)
                g3 = gevent.spawn(pingan_request, pingansession, cdt)
                g4 = gevent.spawn(get_premium, ancheng_session, cdt)
                g5 = gevent.spawn(hn_request, hn_session, cdt)
                gevent.joinall([g1, g2, g3, g4, g5])

            if companyId and '4' in companyId:
                g1 = gevent.spawn(getData_CPlateNo, requestcic, cdt)
                g_list.append(g1)

            if companyId and '5' in companyId:
                g2 = gevent.spawn(flow_main, loginRes, cdt)
                g_list.append(g2)

            if companyId and '1' in companyId:
                g3 = gevent.spawn(pingan_request, pingansession, cdt)
                g_list.append(g3)

            if companyId and '12' in companyId:
                g4 = gevent.spawn(get_premium, ancheng_session, cdt)
                g_list.append(g4)

            if companyId and '13' in companyId:
                g5 = gevent.spawn(hn_request, hn_session, cdt)
                g_list.append(g5)

            if g_list:
                gevent.joinall(g_list)

            # hn_request(hn_session,cdt)
            # get_premium(ancheng_session,cdt)
            # flow_main(loginRes,cdt)
            # pingan_request(pingansession,cdt)
            # getData_CPlateNo(requestcic,cdt)
            print u"程序结束运行%s" % datetime.datetime.now()
            # g2 = gevent.spawn(getData_CPlateNo, requestcic,cdt)
            # g3 = gevent.spawn(flow_main, loginRes,cdt)
            # g4 = gevent.spawn(pingan_request, pingansession,cdt)
            # gevent.joinall([g2,g3,g4])
            # pingan_request(pingansession,cdt)
            # getData_CPlateNo(requestcic,cdt)
        except Exception, e:
            log.error(e)
            log.error(traceback.format_exc())

    except Exception as e:
        log.error(e)
        log.error(traceback.format_exc())
    log.info(u"程序结束运行%s" % datetime.datetime.now())
